package com.uyayo.manager.context.config;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

/**
 * @author hufengjun
 * @since 2017-5-1 11:16
 */

@EnableCaching
@Configuration
public class RedisConfig{
	
	@Value("${redis.cluster.nodes}")
	private String redisClusterNodes;
	
	@Value("${redis.cluster.enable}")
	private boolean redisClusterEnable;
	
    @Bean
    public JedisCluster getJedisCluster(){
    	Set<HostAndPort> nodes = new HashSet<>();
    	if(redisClusterEnable){
    		String[] clusterNodes = redisClusterNodes.split(",");
    		for (int i = 0; i < clusterNodes.length; i++) {
        		String[] hp = clusterNodes[i].split(":");
        		nodes.add(new HostAndPort(hp[0], Integer.valueOf(hp[1])));
    		}
    	}
    	JedisCluster jedisCluster = new JedisCluster(nodes);
    	return jedisCluster;
    }
}